package com.topfinance.cnaps2.web.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.topfinance.cnaps2.dto.SysUserDto;
import com.topfinance.cnaps2.ebo.SysUserEbo;
import com.topfinance.cnaps2.service.ISysUserManager;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class SysUserAction extends BaseSupport implements ModelDriven<SysUserDto> {

	private ISysUserManager userManager = null;

	private SysUserDto userDto = new SysUserDto();

	public SysUserDto getModel() {
		return userDto;
	}

	public void setSysUserManager(ISysUserManager userManager) {
		this.userManager = userManager;
	}

	public SysUserAction() {
		super();
	}

	public String execute() {
		return list();
	}
	public String list() {
		log.debug("SysUserAction-list begin");
		try {
			String oid = request.getParameter("oid");
			List l = userManager.getAllUserList(oid);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysUserAction-list end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(userDto));
			userManager.addUser(userDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + userDto.getID();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(userDto));
			userManager.editUser(userDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			String delId = request.getParameter("id");
			userManager.deleteUser(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public String login() {
		try {
			String loginname = request.getParameter("loginname");
			String password = request.getParameter("password");
			SysUserEbo user = userManager.checkLoginUser(loginname, password);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", Boolean.toString(user != null));
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			//rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public String test() {
		log.debug("SysUserAction-test begin");
		try {
			SysUserDto uDto = new SysUserDto();
			uDto.setName("测试部门2");
			uDto.setLoginname("U1002");
			uDto.setPassword("password");
			uDto.setOID(1102);
			uDto.setCreateDate(new Date());
			userManager.addUser(uDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysUserAction-list end");
		return SUCCESS;
	}	
}
